package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.PeachBlossomApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Sound;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author WZQ
 * @date 2022/1/6
 * @TIME:20:11
 */
@Service
public class PeachBlossomService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer webServer;
    @DubboReference
    private PeachBlossomApi peachBlossomApi;
    @DubboReference
    UserInfoApi userInfoApi;

    public void publishSound(MultipartFile soundFile,Integer count) throws IOException {
        // 判断文件类型是否是语音
        String filename = soundFile.getOriginalFilename();
        String fileType = filename.substring(filename.lastIndexOf(".") + 1);
        String type = "m4a";
        if (!fileType.equals(type)) {
            throw new BusinessException(new ErrorResult("99999", "文件类型不匹配"));
        }
        // 获取音频存入地址
        StorePath storePath = fastFileStorageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), filename, null);
        String videoUrl = webServer.getWebServerUrl() + storePath.getFullPath();
        peachBlossomApi.save(videoUrl, UserHolder.getUserId());
        if (count == null) {
            count = 1;
        }
        count++;
        // 存入发送次数
        redisTemplate.opsForHash().put("SOUND",Constants.SOUND_USE_KEY+UserHolder.getUserId(),count);
    }

    public Map ListenSound(Long userId,Integer count) {
        Sound sound = peachBlossomApi.randomSound(userId);
        // 获取音频用户id并且修改听取记录为以听取
        Long SoundUserId = sound.getUserId();
        UserInfo soundUser = userInfoApi.findById(SoundUserId);
        // 将语音信息封装在map中
        Map<String, Object> map = new HashMap<>();
        map.put("id",soundUser.getId());
        map.put("avatar",soundUser.getAvatar());
        map.put("nickname",soundUser.getNickname());
        map.put("gender",soundUser.getGender());
        map.put("age",soundUser.getAge());
        map.put("soundUrl",sound.getSoundUrl());
        // 判断是否为第一次听取语音记录
        if (count == null) {
            count = 1;
        }
        count++;
        // 存入听取的记录
        redisTemplate.opsForHash().put("SOUND",Constants.SOUND_USER_KEY+userId,count);
        map.put("remainingTimes",10-count);
        return map;
    }
}
